//
//  ViewController.m
//  changAnCommercial
//
//  Created by 高文富 on 15/11/20.
//  Copyright © 2015年 高文富. All rights reserved.
//

#import "ViewController.h"
#import "MBProgressHUD+Add.h"
#import "JavaScriptInterface.h"

@interface ViewController (){
    NSString *htmlValue;
    MBProgressHUD *hub;
}
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
     self.navigationController.navigationBar.hidden = YES;
    _webView.delegate = self;
    _webView.dataDetectorTypes = UIDataDetectorTypeLink;
    _webView.opaque = NO;
    _webView.backgroundColor = [UIColor clearColor];
    
    JavaScriptInterface* interface = [JavaScriptInterface new];
    interface.delegate=self;
    interface.dicValue=_dicValue;
    [_webView addJavascriptInterfaces:interface WithName:@"JavaScriptInterface"];
    NSLog(@"--------%@",[_dicValue objectForKey:@"serverAddress"]);
   
     NSURL *url=[NSURL URLWithString:[_dicValue objectForKey:@"serverAddress"]];
     NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url
                             
                                                  cachePolicy:NSURLRequestReloadIgnoringCacheData
                             
                                              timeoutInterval:20];
    

    [_webView loadRequest:request];
}

-(void)viewWillAppear:(BOOL)animated{
    
    self.navigationController.navigationBar.hidden = YES;
}

- (void)webViewDidStartLoad:(UIWebView *)webView{

      hub= [MBProgressHUD showMessag:@"请稍等" toView:webView];
    
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{

    
    if ([error code] == NSURLErrorCancelled){
        [hub hide:YES afterDelay:0];
    }else{
    
        [hub hide:YES afterDelay:0];
        [MBProgressHUD showError:@"服务器错误,请重新连接！" toView:webView];
        [self performSelector:@selector(backHome) withObject:nil afterDelay:1.2f];
    }
   
  
}

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
     [hub hide:YES afterDelay:0];

    
    JSContext *context = [_webView  valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    context[@"upDataService"] = ^() {
        
       [self updateValue];
        
    };
    context[@"exit"] = ^() {
        
        [self exitClearValue];

    };
    

   

}
#pragma mark - UIWebViewDelegate
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{

    return YES;
}

//存储值
-(void)putTo:(NSString *)key :(NSString *)velue{

    if (_delegate && [_delegate respondsToSelector:@selector(updateValue:Value:)])
    {
        [_delegate updateValue:key Value:velue];
        
    }
  
}


//更新值
-(void)updateValue{

  [self backHome];
}

-(void)exitClearValue{

    if (_delegate && [_delegate respondsToSelector:@selector(exitClearValue)])
    {
        [_delegate exitClearValue];
    }
    [self backHome];

}


-(void)backHome{

    [self.navigationController popViewControllerAnimated:YES];
}


-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
